package com.example.javaeefinal.controller;

import com.example.javaeefinal.Service.UserService;
import com.example.javaeefinal.pojo.User;
import com.example.javaeefinal.utils.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //    注册
    @PostMapping("/register")
    public Result register(String username, String password) {
        User u = userService.findPasswordByUserName(username);
        if (u == null) {
            userService.register(username, password);
            return Result.success();
        } else {
            return Result.error("1", "用户名已被占用");
        }

    }

    //登录
    @PostMapping("/login")
    @ResponseBody
    public Result<String> login(HttpServletRequest request, String logname, String password) {
        User u = userService.findPasswordByUserName(logname);
        if (u == null) {
            return Result.error("1", "用户名错误");
        }
        if (password.equals(u.getPassword())) {
            u.setPassword(null);
            request.getSession().setAttribute("user", u);
            return Result.success();
        }

        return Result.error("1", "密码错误");
    }

    //    用户查询  根据手机号或者姓名模糊查找
    @GetMapping("/findUserList")
    @ResponseBody
    public Result<Object> findUserList(int type, String value, int pageSize, int pageNumber) {
        return Result.success(userService.findUserList(type, value, pageSize, pageNumber));
    }


    //成员查询 根据角色id得到对应角色的用户列表
    @GetMapping("/findUsersRid")
    @ResponseBody
    public Result<List<User>> findUsersRid(int roleId) {
        return Result.success("0", userService.findUsersRid(roleId));
    }

    //添加成员  根据用户id，角色id将对应的角色赋予给对应用户
    @PostMapping("/addRole")
    public Result addRole(int userId, int roleId) {
        User u = userService.isExistByUid(userId);
        if (u != null) {
            userService.addRole(userId, roleId);
            return Result.success();
        } else {
            return Result.error("1", "该用户不存在");
        }
    }

    //删除成员 根据用户id，角色id将对应的角色删除
    @GetMapping("/delRole")
    public Result delRole(int userId, int roleId) {
        User u = userService.isExistByUid(userId);
        if (u != null) {
            userService.delRole(userId, roleId);
            return Result.success();
        } else {
            return Result.error("1", "该用户不存在");
        }
    }

    //查找信息（用户信息管理
    @GetMapping("/getUserById")
    @ResponseBody
    public Result<User> findUserById(int id) {
        User user = userService.findUserById(id);
        return user == null ? Result.error("1", "查找失败") : Result.success("0", user);
    }

    //更新信息（用户信息管理
    @PostMapping("/updateUser")
    @ResponseBody
    public Result updateUserById(@RequestBody User user) {
        userService.updateUserAndInfoById(user);
        return Result.success();
    }

    //创建新用户
    @PostMapping("/createNewUser")
    @ResponseBody
    public Result createNewUser(@RequestBody User user) {

        return Result.success();
    }
}
